/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.gobblin.runtime;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.util.Collections;

import org.apache.gobblin.configuration.ConfigurationKeys;
import org.apache.gobblin.configuration.DynamicConfigGenerator;
import org.apache.gobblin.util.ClassAliasResolver;
import org.apache.gobblin.util.ConfigUtils;
import org.apache.gobblin.util.reflection.GobblinConstructorUtils;


/**
 * For getting an instance of a {@link DynamicConfigGenerator}
 */
public class DynamicConfigGeneratorFactory {
  /**
   * Get an instance of a {@link DynamicConfigGenerator}
   * @param config {@link Config} to pass to the constructor
   * @return an instance of {@link DynamicConfigGenerator}
   */
  public static DynamicConfigGenerator createDynamicConfigGenerator(Config config) {
    String dynamicConfigGeneratorClassName =
        ConfigUtils.getString(config, ConfigurationKeys.DYNAMIC_CONFIG_GENERATOR_CLASS_KEY,
            ConfigurationKeys.DEFAULT_DYNAMIC_CONFIG_GENERATOR_CLASS_KEY);

    try {
      ClassAliasResolver<DynamicConfigGenerator> aliasResolver =
          new ClassAliasResolver<>(DynamicConfigGenerator.class);
      return aliasResolver.resolveClass(dynamicConfigGeneratorClassName).newInstance();
    } catch (ReflectiveOperationException e) {
      throw new RuntimeException("Could not construct DynamicConfigGenerator " +
          dynamicConfigGeneratorClassName, e);
    }
  }
}
